﻿// Picturez
//
// Copyright © Frank Nagl, 2008-2012
// admin@franknagl.de
// www.franknagl.de
//
namespace Picturez
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using ComponentFactory.Krypton.Toolkit;

    /// <summary> Provides GUI for entering a string in a text box.</summary>
    public partial class MessageBoxOk : KryptonForm
    {
        /// <summary>
        /// Initializes a new instance of the 
        /// <see cref="MessageBoxOk"/> class.
        /// </summary>
        /// <param name="caption">
        /// The caption of the <see cref="MessageBoxOk"/> window.</param>
        /// <param name="text">The text to show.</param>
        /// <param name="closeAutomatically">Determines whether the message 
        /// box will be closed automatically.</param>
        protected MessageBoxOk(
            string text,
            string caption,
            bool closeAutomatically)
        {
            InitializeComponent();
            Init(text, caption);

            if (closeAutomatically)
            {
                Timer t1 = new Timer();
                t1.Interval = 3000;
                t1.Tick += delegate(object sender, EventArgs e)
                {
                    Timer t = (Timer)sender;
                    t.Stop();
                    t.Dispose();
                    Close();
                };
                t1.Start();
            }
        }

        private void Init(string text, string caption)
        {
            Text = caption;
            int n = text.Length / 30;
            for (int i = 1; i <= n; i++)
            {
                int spaceIndex = text.IndexOf(' ', i * 30) + 1;
                if (spaceIndex != 0)
                    text = text.Insert(spaceIndex, Environment.NewLine);

                // adapt size of message box
                const int value = 15;
                Height = Height + value;
                MainkryptonPanel.Height = MainkryptonPanel.Height + value;
                btnOK.Location = btnOK.Location + new Size(0, value);
            }
            label.Text = text;    

        }

        /// <summary> Shows the string dialog. </summary>
        /// <param name="caption">
        /// The caption of the <see cref="MessageBoxOkCancel"/> window.</param>
        /// <param name="text">The text to show.</param>
        /// <param name="closeAutomatically">Determines whether the message 
        /// box will be closed automatically.</param>
        /// <returns>The dialog result after user interaction.</returns>
        public static DialogResult Show(
            string text,
            string caption = "",
            bool closeAutomatically = false)
        {
            MessageBoxOk dialog = new MessageBoxOk(
                                            text, caption, closeAutomatically);
            DialogResult dr = dialog.ShowDialog();
            return dr;
        }
    }
}
